rm(list = setdiff(ls(), lsf.str()))
#B.1: systematic drop-out------------------------
load("Study 2/data_trump.RData")
data_trump$income <- as.numeric(data_trump$income)
drop_out<-glm(conjoint ~ zero1(Agre) + zero1(authoritarianism) + zero1(partyidentity) + sex + age + as.factor(education) + income, data=data_trump, family="binomial")

#Load Long dataset------------
load("Study 2/Study2_dataset.RData")

data_long.m$Agreeable<-zero1(data_long.m$Agreeable)
data_long.m$Authoritarian<-zero1(data_long.m$Authoritarian)
data_long.m$agree_cat<-0
data_long.m$agree_cat[data_long.m$Agreeable>.5 & data_long.m$Agreeable<=0.688]=1
data_long.m$agree_cat[data_long.m$Agreeable<=.5]=2
levels(data_long.m$agree_cat)[0] <- "High"
levels(data_long.m$agree_cat)[1] <- "Modest"
levels(data_long.m$agree_cat)[2] <- "Low"

#Subset data for analyeses of feelings
data_long.m$feeling<-as.numeric(data_long.m$feeling)
data_long.m.feel<-na.omit(data_long.m) #exclude missing on the feeling dimension

#B.2: Randomization check -----------------------------------------------------
model.votechoice <- lm(zero1(Agreeable) ~ anti + represent+  cooperate + immi2 + immi3 + immi4 + tax2 + tax3 + tax4 + as.factor(background) + round, data=data_long.m)
model.votechoice.output <- super.cluster.fun(model.votechoice, data_long.m$id)
robust.se1 <- model.votechoice.output[[1]][,2]

model.feeling <- lm(zero1(Authoritarian) ~ anti + represent+  cooperate + immi2 + immi3 + immi4 + tax2 + tax3 + tax4 + as.factor(background) + round, data=data_long.m.feel)
model.feeling.output <- super.cluster.fun(model.feeling, data_long.m.feel$id)
robust.se2 <- model.feeling.output[[1]][,2]

#Extract results
stargazer(model.votechoice, model.feeling, title="Conjoint experiment: randomization checks", align=TRUE, 
          dep.var.labels=c("Agreeableness", "Authoritarianism"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes = "OLS Regression models; *p<0.05", 
          notes.append = FALSE, 
          covariate.labels = c("Anti-establishment", "People's centrism", "Conflict", "Immi 2: Immigration is good for economy","Immi 3: Immigrants steal jobs", "Immi 4: America is for American", "Economy 2: Tax all Americans", "Economy 3: Rich contribute more", "Economy 4: No solution to lower the deficit", "Background 2: Economic advisor of government","Background 3: Management consultant", "Background 4: Social worker", "Round 2"),
          no.space=TRUE, label="tab:randomization", 
          se=list(robust.se1, robust.se2), out="Tables/randomcheck.tex")

#B.3: Main effect -----------------------------------------------------
model.votechoice <- lm(vote ~ Agreeable + Authoritarian + anti + represent+  cooperate + immi2 + immi3 + immi4 + tax2 + tax3 + tax4 + as.factor(background) + round, data=data_long.m)
model.votechoice.output <- super.cluster.fun(model.votechoice, data_long.m$id)
robust.se1 <- model.votechoice.output[[1]][,2]

model.feeling <- lm(zero1(feeling) ~ Agreeable + Authoritarian + anti + represent+  cooperate + immi2 + immi3 + immi4 + tax2 + tax3 + tax4 + as.factor(background) + round, data=data_long.m.feel)
model.feeling.output <- super.cluster.fun(model.feeling, data_long.m.feel$id)
robust.se2 <- model.feeling.output[[1]][,2]

#Extract results
stargazer(model.votechoice, model.feeling, title="Conjoint experiment: main effects", align=TRUE, 
          dep.var.labels=c("Vote Choice", "Feeling"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes = "OLS Regression models; *p<0.05", 
          notes.append = FALSE, 
          covariate.labels = c("Agreeableness", "Authoritarianism",  "Anti-establishment", "People's centrism", "Conflict", "Immi 2: Immigration is good for economy","Immi 3: Immigrants steal jobs", "Immi 4: America is for American", "Economy 2: Tax all Americans", "Economy 3: Rich contribute more", "Economy 4: No solution to lower the deficit", "Background 2: Economic advisor of government","Background 3: Management consultant", "Background 4: Social worker", "Round 2"),
          no.space=TRUE, label="tab:maineffect", 
          se=list(robust.se1, robust.se2), out="Tables/conjoint_votechoice_reg1.tex")

#B.4: Results belonging to Figure 2-----------------------------------------------------
model.votechoice_int <- lm(vote ~ Agreeable*anti+Agreeable*represent + Agreeable*cooperate+Agreeable*immi2+Agreeable*immi3+Agreeable*immi4+ Agreeable*tax2+ Agreeable*tax3+ Agreeable*tax4+as.factor(background)+Authoritarian*anti+Authoritarian*represent+Authoritarian*cooperate+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4+ round, data=data_long.m)
summary(model.votechoice_int)
model.votechoice.output <- super.cluster.fun(model.votechoice_int, data_long.m$id)
robust.se1 <- model.votechoice.output[[1]][,2]
#predict(model.votechoice_int, value_min2SD, interval="predict") 

model.votechoice_int_feel <- lm(zero1(feeling) ~  Agreeable*anti+Agreeable*represent + Agreeable*cooperate++Agreeable*immi2+Agreeable*immi3+Agreeable*immi4+ Agreeable*tax2+ Agreeable*tax3+ Agreeable*tax4+as.factor(background)+Authoritarian*anti+Authoritarian*represent+Authoritarian*cooperate+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4+ round, data=data_long.m.feel)
model.votechoice.output.feel <- super.cluster.fun(model.votechoice_int_feel, data_long.m.feel$id)
robust.se2 <- model.votechoice.output.feel[[1]][,2]

#Extract results
stargazer(model.votechoice_int, model.votechoice_int_feel, single.row=T, title="Study 2: Effect of Anti-establishment messages on Vote Choice and Feelings contiditional upon Agreeableness", align=TRUE, 
          dep.var.labels=c("Vote Choice", "Feeling"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes = "Unstandardized OLS coefficient; *p<0.05", 
          notes.append = FALSE, 
          covariate.labels = c("Agreeableness", "Anti-establishment", "People's centrism", "Conflict", "Immi 2: Immigration is good for economy","Immi 3: Immigrants steal jobs", "Immi 4: America is for American", "Economy 2: Tax all Americans", "Economy 3: Rich contribute more", "Economy 4: No solution to lower the deficit", "Background 2: Economic advisor of government","Background 3: Management consultant", "Background 4: Social worker", "Authoritarianism", "Round 2", "Agreeableness * Anti-establishment", "Agreeableness * People's centrism", "Agreeableness * Conflict", "Agreeableness * Immigration 2", "Agreeableness * Immigration 3", "Agreeableness * Immigration 4", "Agreeableness * Economy 2", "Agreeableness * Economy 3", "Agreeableness * Economy 4", "Authoritarianism * Anti-establishment", "Authoritarianism * People's centrism", "Authoritarianism * Conflict", "Authoritarianism * Immigration 2", "Authoritarianism * Immigration 3", "Authoritarianism * Immigration 4", "Authoritarianism * Economy 2", "Authoritarianism * Economy 3", "Authoritarianism * Economy 4", "Intercept"),
          no.space=TRUE,
          se=list(robust.se1, robust.se2), out="Tables/Figure2_results.tex", label="tab:conjoint")

#Figure 2 main text--------------
fig2feel.a <- interplot(m = model.votechoice_int, var1 = "anti", var2 = "Agreeable", plot=FALSE)
fig2feel.b <- interplot(m = model.votechoice_int, var1 = "cooperate", var2 = "Agreeable", plot=FALSE)
fig2feel.c <- interplot(m = model.votechoice_int, var1 = "represent", var2 = "Agreeable", plot=FALSE)

fig2.data <-rbind(fig2feel.a,fig2feel.b,fig2feel.c)
fig2.data$z <- factor(c(rep("Anti-establishment",33),rep("Conflict",33),
                        rep("People-centrism",33)), levels=c("Anti-establishment", "People-centrism", "Conflict"))

#fig2 <- ggarrange(fig2.a,fig2.b,fig2.c, labels=c("Anti-establishment", "Cooperation", "People-centrism")
fig2 <- ggplot(fig2.data, aes(x=Agreeable, y=coef)) +
  geom_line() +
  geom_ribbon(aes(ymin=lb, ymax=ub), alpha=.2, fill="blue") +
  facet_wrap(~z) +
  geom_hline(yintercept = 0)+theme_bw()+ylab('Marginal effect on vote choice')+xlab('Agreeableness')

ggsave(fig2, file="figures/fig2.pdf", dpi=900, width = 8, height =4)

#B.4: Figure belonging to the self-reported feelings appendix --------------
fig2.a <- interplot(m = model.votechoice_int_feel, var1 = "anti", var2 = "Agreeable", plot=FALSE)
fig2.b <- interplot(m = model.votechoice_int_feel, var1 = "cooperate", var2 = "Agreeable", plot=FALSE)
fig2.c <- interplot(m = model.votechoice_int_feel, var1 = "represent", var2 = "Agreeable", plot=FALSE)

fig2.data <-rbind(fig2.a,fig2.b,fig2.c)
fig2.data$z <- factor(c(rep("Anti-establishment",33),rep("Conflict",33),
                        rep("People-centrism",33)), levels=c("Anti-establishment", "People-centrism", "Conflict"))

#fig2 <- ggarrange(fig2.a,fig2.b,fig2.c, labels=c("Anti-establishment", "Cooperation", "People-centrism")
fig2 <- ggplot(fig2.data, aes(x=Agreeable, y=coef)) +
  geom_line() +
  geom_ribbon(aes(ymin=lb, ymax=ub), alpha=.2, fill="blue") +
  facet_wrap(~z) +
  geom_hline(yintercept = 0)+theme_bw()+ylab('Marginal effect on vote choice')+xlab('Agreeableness')

ggsave(fig2, file="Figures/fig2_feelings.pdf", dpi=900, width = 8, height =4)

#B4. Figure Agreeableness X anti-immigration in Appendix
fig_appendix_agree <- interplot(m = model.votechoice_int, var1 = "immi3", var2 = "Agreeable", hist=TRUE, ci=0.9, rfill="blue", ralpha=.2)+geom_hline(yintercept = 0)+theme_minimal()+ylab('Marginal effect: Immigrants steal jobs')+xlab('Agreeableness') +   theme(axis.text=element_text(size=14)) + theme(axis.text=element_text(size=14))
ggsave(fig_appendix_agree, file="fig_appendix_agree.pdf", dpi=900)

#B.4: Authoritarianism interactions -------------------------------------------

#Figure Authoritarianism X anti-immigration in Appendix
fig_appendix_auth1 <- interplot(m = model.votechoice_int, var1 = "immi3", var2 = "Authoritarian", hist=TRUE, ci=0.9, rfill="blue", ralpha=.2)+geom_hline(yintercept = 0)+theme_minimal()+ylab('Marginal effect: Immigrants steal jobs')+xlab('Authoritarianism') + theme(axis.text=element_text(size=14)) + theme(axis.text=element_text(size=14))
ggsave(fig_appendix_auth1, file="figures/fig_appendix_auth1.pdf", dpi=900)

fig_appendix_auth2 <- interplot(m = model.votechoice_int, var1 = "immi4", var2 = "Authoritarian", hist=TRUE, ci=0.9, rfill="blue", ralpha=.2)+geom_hline(yintercept = 0)+theme_minimal()+ylab('Marginal effect: America is for Americans')+xlab('Authoritarianism') + theme(axis.text=element_text(size=14)) + theme(axis.text=element_text(size=14))
ggsave(fig_appendix_auth2, file="figures/fig_appendix_auth2.pdf", dpi=900)


#B.5: Testing the assumed linearity --------------
model.votechoice_int <- lm(vote ~as.factor(agree_cat)*anti+as.factor(agree_cat)*represent+as.factor(agree_cat)*cooperate+as.factor(agree_cat)*immi2+as.factor(agree_cat)*immi3+as.factor(agree_cat)*immi4+ as.factor(agree_cat)*tax2+ as.factor(agree_cat)*tax3+ as.factor(agree_cat)*tax4+ as.factor(background)+Authoritarian*anti+Authoritarian*represent+Authoritarian*cooperate+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4+ round, data=data_long.m)
model.votechoice.output <- super.cluster.fun(model.votechoice_int, data_long.m$id)
robust.se1 <- model.votechoice.output[[1]][,2]

model.votechoice_int_feel <- lm(zero1(feeling) ~as.factor(agree_cat)*anti+as.factor(agree_cat)*represent+as.factor(agree_cat)*cooperate+as.factor(agree_cat)*immi2+as.factor(agree_cat)*immi3+as.factor(agree_cat)*immi4+ as.factor(agree_cat)*tax2+ as.factor(agree_cat)*tax3+ as.factor(agree_cat)*tax4+ as.factor(background)+Authoritarian*anti+Authoritarian*represent+Authoritarian*cooperate+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4+ round, data=data_long.m.feel)
model.votechoice.output.feel <- super.cluster.fun(model.votechoice_int_feel, data_long.m.feel$id)
robust.se2 <- model.votechoice.output.feel[[1]][,2]

stargazer(model.votechoice_int, model.votechoice_int_feel, single.row=T, title="Study 2: Testing linearity assumption", align=TRUE,     dep.var.labels=c("Vote Choice", "Feeling"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes = "Unstandardized OLS coefficient; *p<0.05", 
          notes.append = FALSE, 
          covariate.labels = c("Agreeableness: Modest", "Agreeableness: Low", "Anti-establishment", "People's centrism", "Conflict", "Immi 2: Immigration is good for economy","Immi 3: Immigrants steal jobs", "Immi 4: America is for American", "Economy 2: Tax all Americans", "Economy 3: Rich contribute more", "Economy 4: No solution to lower the deficit", "Background 2: Economic advisor of government","Background 3: Management consultant", "Background 4: Social worker", "Authoritarianism", "Round 2", "Agreeableness: Modest * Anti-establishment", "Agreeableness: Low * Anti-establishment", "Agreeableness: Modest * People's centrism", "Agreeableness: Low * People's centrism", "Agreeableness: Modest * Conflict", "Agreeableness: Low * Confict", "Agreeableness: Low * Immigration 2", "Agreeableness: Modest * Immigration 2", "Agreeableness: Low * Immigration 2",   "Agreeableness: Modest * Immigration 3", "Agreeableness: Low * Immigration 3",  "Agreeableness: Modest * Immigration 4", "Agreeableness: Low * Immigration4",  "Agreeableness: Modest * Economy 2", "Agreeableness: Low * Economy 2", "Agreeableness: Modest * Economy 3",  "Agreeableness: Low * Economy 3", "Agreeableness: Modest * Economy 4", "Agreeableness: Low * Economy 4", "Authoritarianism * Anti-establishment", "Authoritarianism * People's centrism", "Authoritarianism * Conflict", "Authoritarianism * Immigration 2", "Authoritarianism * Immigration 3", "Authoritarianism * Immigration 4", "Authoritarianism * Economy 2", "Authoritarianism * Economy 3", "Authoritarianism * Economy 4", "Intercept"),
          no.space=TRUE,
          se=list(robust.se1, robust.se2), out="Tables/Tablesconjoint_votechoice_reg3.tex", label="tab:conjoint_linear" )

#B.6: Populism scale ---------------------------------------------
data_long.m$populism.scale1 <- data_long.m$cooperate + data_long.m$anti + data_long.m$represent
model.votechoice_int.ext1 <- lm(vote ~ Agreeable*populism.scale1+Agreeable*immi2+Agreeable*immi3+Agreeable*immi4+ Agreeable*tax2+ Agreeable*tax3+ Agreeable*tax4+as.factor(background)+Authoritarian*populism.scale1+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4+ round, data=data_long.m)

model.votechoice_int.ext2 <- lm(zero1(feeling) ~ Agreeable*populism.scale1+Agreeable*immi2+Agreeable*immi3+Agreeable*immi4+ Agreeable*tax2+ Agreeable*tax3+ Agreeable*tax4+as.factor(background)+Authoritarian*populism.scale1+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4+ round, data=data_long.m)

fig2scale.a <- interplot(m = model.votechoice_int.ext1, var1 = "populism.scale1", var2 = "Agreeable", plot=FALSE)
fig2scale.b <- interplot(m = model.votechoice_int.ext2, var1 = "populism.scale1", var2 = "Agreeable", plot=FALSE)

fig2.data <-rbind(fig2scale.a,fig2scale.b)
fig2.data$z<-c(rep("Vote choice",33), rep("Feeling",33))
fig2.data$z<-factor(fig2.data$z, levels=c("Vote choice", "Feeling"))

#fig2 <- ggarrange(fig2.a,fig2.b,fig2.c, labels=c("Anti-establishment", "Cooperation", "People-centrism")
fig2 <- ggplot(fig2.data, aes(x=Agreeable, y=coef)) +
  geom_line() + facet_grid(.~z) +
  geom_ribbon(aes(ymin=lb, ymax=ub), alpha=.2, fill="blue")+
  geom_hline(yintercept = 0)+theme_bw()+ylab('Marginal effect of populism scale')+xlab('Agreeableness')

ggsave(fig2, file="Figures/fig2_scale.pdf", dpi=900, width = 8, height =4)


#B.7: Controlling for PID -----------------------------------------------------

model.votechoice_int <- lm(vote ~ Agreeable*anti+Agreeable*cooperate+Agreeable*represent+Agreeable*immi2+Agreeable*immi3+Agreeable*immi4+ Agreeable*tax2+ Agreeable*tax3+ Agreeable*tax4+as.factor(background)+Authoritarian*anti+Authoritarian*cooperate+Authoritarian*represent+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4 + PID*anti+PID*cooperate+PID*represent+PID*immi2+PID*immi3+PID*immi4+ PID*tax2+ PID*tax3+ PID*tax4+ round, data=data_long.m)
model.votechoice.output <- super.cluster.fun(model.votechoice_int, data_long.m$id)
robust.se1 <- model.votechoice.output[[1]][,2]

model.votechoice_int_feel <- lm(zero1(feeling) ~ Agreeable*anti+Agreeable*cooperate+Agreeable*represent+Agreeable*immi2+Agreeable*immi3+Agreeable*immi4+ Agreeable*tax2+ Agreeable*tax3+ Agreeable*tax4+as.factor(background)+Authoritarian*anti+Authoritarian*cooperate+Authoritarian*represent+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4 + PID*anti+PID*cooperate+PID*represent+PID*immi2+PID*immi3+PID*immi4+ PID*tax2+ PID*tax3+ PID*tax4+ round, data=data_long.m.feel)
model.votechoice.output.feel <- super.cluster.fun(model.votechoice_int_feel, data_long.m.feel$id)
robust.se2 <- model.votechoice.output.feel[[1]][,2]

#Extract results
stargazer(model.votechoice_int, single.row=T, title="Vote choice based upon candidate characteristics", align=TRUE,   dep.var.labels=c("Vote Choice", "Feeling"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes = "OLS Regression models; *p<0.05", 
          notes.append = FALSE, 
          covariate.labels = c("Agreeableness", "Anti-establishment", "People's centrism", "Conflict", "Immi 2: Immigration is good for economy","Immi 3: Immigrants steal jobs", "Immi 4: America is for American", "Economy 2: Tax all Americans", "Economy 3: Rich contribute more", "Economy 4: No solution to lower the deficit", "Background 2: Economic advisor of government","Background 3: Management consultant", "Background 4: Social worker", "Authoritarianism", "PID (1-7)", "Round 2", "Agreeableness * Anti-establishment", "Agreeableness * People's centrism", "Agreeableness * Conflict", "Agreeableness * Immigration 2", "Agreeableness * Immigration 3", "Agreeableness * Immigration 4", "Agreeableness * Economy 2", "Agreeableness * Economy 3", "Agreeableness * Economy 4", "Authoritarianism * Anti-establishment", "Authoritarianism * People's centrism", "Authoritarianism * Conflict", "Authoritarianism * Immigration 2", "Authoritarianism * Immigration 3", "Authoritarianism * Immigration 4", "Authoritarianism * Economy 2", "Authoritarianism * Economy 3", "Authoritarianism * Economy 4", "PID * Anti-establishment", "PID * People's centrism", "PID * Conflict", "PID * Immigration 2", "PID * Immigration 3", "PID * Immigration 4", "PID * Economy 2", "PID * Economy 3", "PID * Economy 4", "Intercept"),
          no.space=TRUE,
          se=list(robust.se1), out="Tables/Tablesconjoint_votechoice_reg4.tex", label="tab:conjoint_pid")

pid_immi <- interplot(m = model.votechoice_int, var1 = "immi3", var2 = "PID", hist=TRUE, ci=0.9, rfill="blue", ralpha=.2)+geom_hline(yintercept = 0)+theme_minimal()+ylab('Marginal effect: Immigrants steal job')+xlab('Party Identification') + theme(axis.text=element_text(size=14)) + theme(axis.text=element_text(size=14))
ggsave(pid_immi, file="figures/pid_immi.pdf", dpi=900)

fig2.a <- interplot(m = model.votechoice_int, var1 = "anti", var2 = "PID", plot=FALSE)
fig2.b <- interplot(m = model.votechoice_int, var1 = "cooperate", var2 = "PID", plot=FALSE)
fig2.c <- interplot(m = model.votechoice_int, var1 = "represent", var2 = "PID", plot=FALSE)

fig2.data <-rbind(fig2.a,fig2.b,fig2.c)
fig2.data$z <- factor(c(rep("Anti-establishment",6),rep("Conflict",6),
                        rep("People-centrism",6)), levels=c("Anti-establishment", "People-centrism", "Conflict"))

fig2 <- ggplot(fig2.data, aes(x=PID, y=coef)) +
  geom_line() +
  geom_ribbon(aes(ymin=lb, ymax=ub), alpha=.2, fill="blue") +
  facet_grid(~z) +
  geom_hline(yintercept = 0)+theme_bw()+ylab('Marginal effect on vote choice')+xlab('Partisanship')
ggsave(fig2, file="Figures/fig2_pid_interaction.pdf", dpi=900, width = 8, height =4)


#B.8: sub-groups - Men vs. Woman----------------------------
model.votechoice_int_female <- lm(vote ~ Agreeable*anti+Agreeable*represent + Agreeable*cooperate++Agreeable*immi2+Agreeable*immi3+Agreeable*immi4+ Agreeable*tax2+ Agreeable*tax3+ Agreeable*tax4+as.factor(background)+Authoritarian*anti+Authoritarian*represent+Authoritarian*cooperate+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4+ round, subset(data_long.m, sex==1))

model.votechoice_int_male <- lm(vote ~ Agreeable*anti+Agreeable*represent + Agreeable*cooperate++Agreeable*immi2+Agreeable*immi3+Agreeable*immi4+ Agreeable*tax2+ Agreeable*tax3+ Agreeable*tax4+as.factor(background)+Authoritarian*anti+Authoritarian*represent+Authoritarian*cooperate+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4+ round, subset(data_long.m, sex==0))

fig2.a <- interplot(m = model.votechoice_int_female, var1 = "anti", var2 = "Agreeable", plot=FALSE)
fig2.b <- interplot(m = model.votechoice_int_female, var1 = "cooperate", var2 = "Agreeable", plot=FALSE)
fig2.c <- interplot(m = model.votechoice_int_female, var1 = "represent", var2 = "Agreeable", plot=FALSE)

fig2.data <-rbind(fig2.a,fig2.b,fig2.c)
fig2.data$z <- factor(c(rep("Anti-establishment",32),rep("Conflict",32),
                        rep("People-centrism",32)), levels=c("Anti-establishment", "People-centrism", "Conflict"))

fig2.data$pid<- "Woman"

fig2.d <- interplot(m = model.votechoice_int_male, var1 = "anti", var2 = "Agreeable", plot=FALSE)
fig2.e <- interplot(m = model.votechoice_int_male, var1 = "cooperate", var2 = "Agreeable", plot=FALSE)
fig2.f <- interplot(m = model.votechoice_int_male, var1 = "represent", var2 = "Agreeable", plot=FALSE)

fig2.data.rep <-rbind(fig2.d,fig2.e,fig2.f)
fig2.data.rep$z <- factor(c(rep("Anti-establishment",28),rep("Conflict",28),
                            rep("People-centrism",28)), levels=c("Anti-establishment", "People-centrism", "Conflict"))
fig2.data.rep$pid<- "Men"

fig2.data.pid<-rbind(fig2.data, fig2.data.rep)

#fig2 <- ggarrange(fig2.a,fig2.b,fig2.c, labels=c("Anti-establishment", "Cooperation", "People-centrism")
fig2 <- ggplot(fig2.data.pid, aes(x=Agreeable, y=coef)) +
  geom_line() +
  geom_ribbon(aes(ymin=lb, ymax=ub), alpha=.2, fill="blue") +
  facet_grid(pid~z) +
  geom_hline(yintercept = 0)+theme_bw()+ylab('Marginal effect on vote choice')+xlab('Agreeableness')

ggsave(fig2, file="Figures/fig2_sex.pdf", dpi=900, width = 8, height =4)

#B.8: sub-groups - Young vs. Old----------------------------
data_long.m$age_med<-ifelse(data_long.m$age> 39, 1,0)

model.votechoice_int_dem <- lm(vote ~ Agreeable*anti+Agreeable*represent + Agreeable*cooperate++Agreeable*immi2+Agreeable*immi3+Agreeable*immi4+ Agreeable*tax2+ Agreeable*tax3+ Agreeable*tax4+as.factor(background)+Authoritarian*anti+Authoritarian*represent+Authoritarian*cooperate+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4+ round, subset(data_long.m, age_med==0))

model.votechoice_int_rep <- lm(vote ~ Agreeable*anti+Agreeable*represent + Agreeable*cooperate++Agreeable*immi2+Agreeable*immi3+Agreeable*immi4+ Agreeable*tax2+ Agreeable*tax3+ Agreeable*tax4+as.factor(background)+Authoritarian*anti+Authoritarian*represent+Authoritarian*cooperate+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4+ round, subset(data_long.m, age_med==1))

fig2.a <- interplot(m = model.votechoice_int_dem, var1 = "anti", var2 = "Agreeable", plot=FALSE)
fig2.b <- interplot(m = model.votechoice_int_dem, var1 = "cooperate", var2 = "Agreeable", plot=FALSE)
fig2.c <- interplot(m = model.votechoice_int_dem, var1 = "represent", var2 = "Agreeable", plot=FALSE)

fig2.data <-rbind(fig2.a,fig2.b,fig2.c)
fig2.data$z <- factor(c(rep("Anti-establishment",31),rep("Conflict",31),
                        rep("People-centrism",31)), levels=c("Anti-establishment", "People-centrism", "Conflict"))

fig2.data$pid<- "Young (below median)"

fig2.d <- interplot(m = model.votechoice_int_rep, var1 = "anti", var2 = "Agreeable", plot=FALSE)
fig2.e <- interplot(m = model.votechoice_int_rep, var1 = "cooperate", var2 = "Agreeable", plot=FALSE)
fig2.f <- interplot(m = model.votechoice_int_rep, var1 = "represent", var2 = "Agreeable", plot=FALSE)

fig2.data.rep <-rbind(fig2.d,fig2.e,fig2.f)
fig2.data.rep$z <- factor(c(rep("Anti-establishment",30),rep("Conflict",30),
                            rep("People-centrism",30)), levels=c("Anti-establishment", "People-centrism", "Conflict"))
fig2.data.rep$pid<- "Old (above median)"


fig2.data.pid<-rbind(fig2.data, fig2.data.rep)
fig2.data.rep$pid<-factor(fig2.data.rep$pid, levels=c("Old (above median)", "Young (below median)"))
fig2 <- ggplot(fig2.data.pid, aes(x=Agreeable, y=coef)) +
  geom_line() +
  geom_ribbon(aes(ymin=lb, ymax=ub), alpha=.2, fill="blue") +
  facet_grid(pid~z) +
  geom_hline(yintercept = 0)+theme_bw()+ylab('Marginal effect on vote choice')+xlab('Agreeableness')

ggsave(fig2, file="Figures/fig2_age.pdf", dpi=900, width = 8, height =4)

#B.9: Anti-establishment, Agreeableness and host ideology ---------------------------------------------
data_long.m$tax.right <- as.factor(ifelse(data_long.m$tax==1 | data_long.m$tax==4,1,0))
data_long.m$anti <- as.factor(data_long.m$anti)
data_long.m$represent <- as.factor(data_long.m$represent)
data_long.m$represent <- as.factor(data_long.m$represent)

model.votechoice_int.ext2 <- lm(vote ~ Agreeable*anti*tax.right+Agreeable*represent*tax.right+Agreeable*cooperate*tax.right+Agreeable*immi2+Agreeable*immi3+Agreeable*immi4+as.factor(background)+Authoritarian*anti*tax.right+Authoritarian*represent*tax.right+Authoritarian*cooperate*tax.right+Authoritarian*immi2+Authoritarian*immi3+Authoritarian*immi4+ round, data=data_long.m)
model.votechoice_int.ext2.vote <- super.cluster.fun(model.votechoice_int.ext2, data_long.m$id)
robust.se1 <- model.votechoice_int.ext2.vote[[1]][,2]

data_long.m$immi.right <- as.factor(ifelse(data_long.m$immi3==1 | data_long.m$immi4==1,1,0))
model.votechoice_int.ext3 <- lm(vote ~ Agreeable*anti*immi.right+Agreeable*represent*immi.right+Agreeable*cooperate*immi.right+Agreeable*tax2+ Agreeable*tax3+ Agreeable*tax4+as.factor(background)+Authoritarian*anti*immi.right+Authoritarian*represent*immi.right+Authoritarian*cooperate*immi.right+Authoritarian*represent*immi.right+Authoritarian*immi.right+Authoritarian*tax2+ Authoritarian*tax3+ Authoritarian*tax4+ round, data=data_long.m)

model.votechoice_int.ext3.vote <- super.cluster.fun(model.votechoice_int.ext3, data_long.m$id)
robust.se2 <- model.votechoice_int.ext3.vote[[1]][,2]
summary(model.votechoice_int.ext3)

ext2.plot1 <- plot_model(model.votechoice_int.ext2, type="pred",terms=c("anti [0,1]", "tax.right [0,1]", "Agreeable [0.25]"))
ext3.plot1 <- plot_model(model.votechoice_int.ext3, type="pred",terms=c("anti [0,1]", "immi.right [0,1]", "Agreeable [0.25]"))

ext3.plot1 <- plot_model(model.votechoice_int.ext3, type="pred",terms=c("represent [0,1]", "immi.right [0,1]", "Agreeable [0.25]"))
ext3.plot1 <- plot_model(model.votechoice_int.ext3, type="pred",terms=c("represent [0,1]", "immi.right [0,1]", "Agreeable [0.75]"))

stargazer(model.votechoice_int.ext2, single.row=T, title="Study 2: Populism, Agreeableness and the ``host ideology'': Economic ideology", align=TRUE,     dep.var.labels=c("Vote Choice"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes = "Unstandardized OLS coefficient; *p<0.05", 
          notes.append = FALSE,
          covariate.labels = c("Intercept", "Agreeableness", "Anti-establishment",  "Right-wing taxation", "People's centrism", "Conflict", "Immi 2: Immigration is good for economy","Immi 3: Immigrants steal jobs", "Immi 4: America is for American", "Background 2: Economic advisor of government","Background 3: Management consultant", "Background 4: Social worker", "Authoritarianism", "Round 2", "Agreeableness * Anti-establishment", "Agreeableness * Right-wing taxation", "Anti-establishment * Right-wing taxation", "Agreeableness * People's centrism", "People's Centrism * Right-wing taxation", "Agreeableness * Conflict", "Conflict * Right-wing taxation", "Agreeableness * Immigration 2", "Agreeableness * Immigration 3", "Agreeableness * Immigration 4", "Authoritarianism * Anti-establishment", "Authoritarianism * Right-wing taxation", "Authoritarianism * People's centrism", "Authoritarianism * Conflict", "Authoritarianism * Immigration 2", "Authoritarianism * Immigration 3", "Authoritarianism * Immigration 4", "Agreeableness * Right-wing econ * Anti", "Agreeableness * Right-wing econ * People's", "Agreeableness * Right-wing econ * Conflict", "Authoritarianism * Right-wing econ * Anti", "Authoritarianism * Right-wing econ * People's", "Authoritarianism * Right-wing econ * Conflict"),
          no.space=TRUE, 
          se=list(robust.se1), out="Tables/tables_ideology_econ.tex", label="tab:econ")

stargazer(model.votechoice_int.ext3, single.row=T, title="Study 2: Populism, Agreeableness and the ``host ideology'': Social ideology", align=TRUE,     dep.var.labels=c("Vote Choice"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes = "Unstandardized OLS coefficient; *p<0.05", 
          notes.append = FALSE,
          covariate.labels = c("Intercept", "Agreeableness", "Anti-establishment",  "Anti-immi", "People's centrism", "Conflict", "Economy 2: Tax all Americans", "Economy 3: Rich contribute more", "Economy 4: No solution to lower the deficit", "Background 2: Economic advisor of government","Background 3: Management consultant", "Background 4: Social worker", "Authoritarianism", "Round 2", "Agreeableness * Anti-establishment", "Agreeableness * Anti-immi", "Anti-establishment * Anti-immi", "Agreeableness * People's centrism", "People's Centrism * Anti-immi", "Agreeableness * Conflict", "Conflict *Anti-immi", "Agreeableness * Economy 2", "Agreeableness * Economy 3", "Agreeableness * Economy 4", "Authoritarianism * Anti-establishment", "Authoritarianism * Anti-immi", "Authoritarianism * People's centrism", "Authoritarianism * Conflict", "Authoritarianism * Economy 2", "Authoritarianism * Economy 3", "Authoritarianism * Economy 4", "Agreeableness * Anti-immi * Anti", "Agreeableness * Anti-immi * People's", "Agreeableness * Anti-immi * Conflict", "Authoritarianism * Anti-immi * Anti", "Authoritarianism * Anti-immi * People's", "Authoritarianism * Anti-immi * Conflict"),
          no.space=TRUE, 
          se=list(robust.se2), out="Tables/tables_ideology_social.tex", label="tab:social")


